package com.syxp.pda.action.detail;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.DefineIDZone;
import com.syxp.pda.entity.base.DeviceBaseInfo;
import com.syxp.pda.entity.base.Profess;
import com.syxp.pda.entity.standard.DeviceCheckStandard;
import com.syxp.pda.service.base.ProfessService;
import com.syxp.pda.service.detail.PointsAnalysisService;
import com.syxp.pda.service.standard.DeviceCheckStandardService;

public class PointsAnalysisAction extends BaseAction {

	private int nodeDepth;

	private String nodeId;

	private String startTime;

	private String endTime;

	private String precision;

	private String dispose;

	private String chkItem;

	private String deviceIDs;

	public String getDeviceIDs() {
		return deviceIDs;
	}

	public void setDeviceIDs(String deviceIDs) {
		this.deviceIDs = deviceIDs;
	}

	@Resource
	private ProfessService professService;

	@Resource
	private DeviceCheckStandardService deviceCheckStandardService;

	@Resource
	PointsAnalysisService pointsAnalysisService;

	public PointsAnalysisService getPointsAnalysisService() {
		return pointsAnalysisService;
	}

	public void setPointsAnalysisService(
			PointsAnalysisService pointsAnalysisService) {
		this.pointsAnalysisService = pointsAnalysisService;
	}

	public String getChkItem() {
		return chkItem;
	}

	public void setChkItem(String chkItem) {
		this.chkItem = chkItem;
	}

	public int getNodeDepth() {
		return nodeDepth;
	}

	public void setNodeDepth(int nodeDepth) {
		this.nodeDepth = nodeDepth;
	}

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getPrecision() {
		return precision;
	}

	public void setPrecision(String precision) {
		this.precision = precision;
	}

	public String getDispose() {
		return dispose;
	}

	public void setDispose(String dispose) {
		this.dispose = dispose;
	}

	public ProfessService getProfessService() {
		return professService;
	}

	public void setProfessService(ProfessService professService) {
		this.professService = professService;
	}

	public DeviceCheckStandardService getDeviceCheckStandardService() {
		return deviceCheckStandardService;
	}

	public void setDeviceCheckStandardService(
			DeviceCheckStandardService deviceCheckStandardService) {
		this.deviceCheckStandardService = deviceCheckStandardService;
	}

	public String loadtree() {
		List list = new ArrayList();
		try {
			int id = Integer.parseInt(this.nodeId.split("#")[this.nodeDepth]);
			if (nodeDepth == 0) {// 点击根节点 获取专业
				List<Profess> professes = this.professService.getDao().getAll();
				for (Profess o : professes) {
					Map map = new HashMap();
					map.put("id", this.nodeId + "#" + o.getProfessID());
					map.put("text", o.getProfessName());
					list.add(map);
				}
			} else if (this.nodeDepth == 1) {// 点击第二层专业节点 获取区域
				List<DefineIDZone> zones = this.deviceCheckStandardService
						.getZones(id);
				for (DefineIDZone o : zones) {
					Map map = new HashMap();
					map.put("id", this.nodeId + "#" + o.getZoneID());
					map.put("text", o.getZoneName());
					list.add(map);
				}
			} else if (this.nodeDepth == 2) {// 点击第三层区域节点 获取设备
				List<DeviceBaseInfo> devices = this.deviceCheckStandardService
						.getDevices(id);
				for (DeviceBaseInfo o : devices) {
					Map map = new HashMap();
					map.put("id", this.nodeId + "#" + o.getDeviceInfoID());
					map.put("text", o.getDeviceName());
					map.put("checked", false);
					map.put("leaf", true);
					list.add(map);
				}
			} else if (this.nodeDepth == 3) {// 点击第四层设备节点 获取巡检项目
				System.out.println("-----------" + deviceIDs);
				if (deviceIDs != null&&!deviceIDs.equals("")) {
					String[] temps = deviceIDs.split(",");
					Integer[] deviceID = new Integer[temps.length];
					for (int i = 0; i < temps.length; i++) {
						String[] tempStr = temps[i].split("#");
						deviceID[i] = Integer
								.valueOf(tempStr[tempStr.length - 1]);
					}

					List<Object[]> objs = pointsAnalysisService
							.loadCHKItem(deviceID);
					;
					for (Object[] o : objs) {
						Map map = new HashMap();
						map.put("chkItem", o[0]);
						map.put("chkTypeName", o[1]);
						list.add(map);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.jsonString = this.gson.toJson(list);
		System.out.println(this.jsonString);
		return SUCCESS;
	}

	public String analysis() {
		try {
			if(deviceIDs != null&&!deviceIDs.equals("")) {
				String[] temps = deviceIDs.split(",");
				Integer[] deviceID = new Integer[temps.length];
				for (int i = 0; i < temps.length; i++) {
					String[] tempStr = temps[i].split("#");
					deviceID[i] = Integer
							.valueOf(tempStr[tempStr.length - 1]);
				}
				List<Object[]> objs = pointsAnalysisService.analysis(deviceID,
						chkItem, startTime, endTime, precision);
				JsonArray jarray = new JsonArray();
				for (Object[] obj : objs) {
					JsonObject jsobj = new JsonObject();
					jsobj.addProperty("devName", String.valueOf(obj[0]));
					jsobj.addProperty("chkTime",
							TimeFormatUtil.formatDateF((Timestamp) obj[1]));
					jsobj.addProperty("avgValue", String.valueOf(obj[2]));
					jarray.add(jsobj);
				}
				JsonObject jsonObj = new JsonObject();
				jsonObj.add("res", jarray);
				jsonObj.addProperty("success", true);
				this.jsonString = gson.toJson(jsonObj);
				System.out.println(this.jsonString);
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.jsonString = "{success:false}";
			
		}
		return SUCCESS;
	}
}
